import * as THREE from 'three';
import { Vec3 } from '@/core/types';
import { PassiveEntity } from './PassiveEntity';
import { EntityType, EntityStats } from './types';
import { WorldManager } from '@/world/WorldManager';
import { ItemDropManager } from '@/world/ItemDrop';
import { BlockType } from '@/blocks/types';
import { getAudioManager } from '@/audio/AudioManager';

export class Cow extends PassiveEntity {
  constructor(id: string, position: Vec3, worldManager: WorldManager, itemDropManager?: ItemDropManager) {
    const stats: EntityStats = {
      maxHealth: 10,
      moveSpeed: 1.5,
      jumpHeight: 1.0,
      detectionRange: 8,
      size: { width: 1.4, height: 1.4 }
    };

    super(id, EntityType.COW, position, worldManager, stats, itemDropManager);
  }

  protected createMesh(): void {
    // 创建一个简单的牛模型
    const group = new THREE.Group();

    // 身体
    const bodyGeometry = new THREE.BoxGeometry(1.4, 0.8, 0.8);
    const bodyMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 }); // 棕色
    const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
    body.position.set(0, 0.4, 0);
    group.add(body);

    // 头部
    const headGeometry = new THREE.BoxGeometry(0.6, 0.6, 0.6);
    const headMaterial = new THREE.MeshLambertMaterial({ color: 0xA0522D }); // 稍浅的棕色
    const head = new THREE.Mesh(headGeometry, headMaterial);
    head.position.set(0, 0.7, -0.7);
    group.add(head);

    // 腿部
    const legGeometry = new THREE.BoxGeometry(0.2, 0.6, 0.2);
    const legMaterial = new THREE.MeshLambertMaterial({ color: 0x654321 }); // 深棕色
    
    const positions = [
      [-0.4, -0.3, -0.3], // 前左腿
      [0.4, -0.3, -0.3],  // 前右腿
      [-0.4, -0.3, 0.3],  // 后左腿
      [0.4, -0.3, 0.3]    // 后右腿
    ];

    positions.forEach(pos => {
      const leg = new THREE.Mesh(legGeometry, legMaterial);
      leg.position.set(pos[0], pos[1], pos[2]);
      group.add(leg);
    });

    // 添加简单的斑点
    const spotGeometry = new THREE.SphereGeometry(0.1, 8, 8);
    const spotMaterial = new THREE.MeshLambertMaterial({ color: 0x000000 }); // 黑色斑点
    
    for (let i = 0; i < 5; i++) {
      const spot = new THREE.Mesh(spotGeometry, spotMaterial);
      spot.position.set(
        (Math.random() - 0.5) * 1.2,
        0.2 + Math.random() * 0.4,
        (Math.random() - 0.5) * 0.6
      );
      group.add(spot);
    }

    // 设置阴影
    group.traverse((child) => {
      if (child instanceof THREE.Mesh) {
        child.castShadow = true;
        child.receiveShadow = true;
      }
    });

    this.mesh = group;
  }

  protected dropItems(): void {
    console.log(`Cow ${this.id} dropped leather and raw beef`);
    
    if (this.itemDropManager) {
      // 掉落皮革 (1-3个)
      const leatherCount = 1 + Math.floor(Math.random() * 3);
      this.itemDropManager.dropItem(BlockType.LEATHER, leatherCount, this.position);
      
      // 掉落生牛肉 (1-3个)
      const beefCount = 1 + Math.floor(Math.random() * 3);
      this.itemDropManager.dropItem(BlockType.RAW_BEEF, beefCount, this.position);
    }
  }

  public update(deltaTime: number): void {
    super.update(deltaTime);
    
    // 添加牛特有的行为
    this.addCowBehavior(deltaTime);
  }

  private addCowBehavior(deltaTime: number): void {
    // 偶尔发出牛叫声
    if (Math.random() < 0.001) { // 很低的概率
      console.log(`Cow ${this.id}: Moo!`);
      this.playIdleSound();
    }

    // 简单的头部摆动动画
    if (this.mesh) {
      const head = this.mesh.children[1]; // 头部是第二个子对象
      if (head) {
        head.rotation.y = Math.sin(this.age * 2) * 0.1;
      }
    }
  }

  private playIdleSound(): void {
    const audioManager = getAudioManager();
    audioManager.playSound('cow_idle', {
      position: this.position,
      volume: 0.5
    });
  }

  // 牛可以被小麦吸引
  public canBeAttractedBy(item: string): boolean {
    return item === 'wheat';
  }
}